/**
 * Created by WLG on 2018/11/12.
 */
//require('../testEnvironment');
const mongoose = require('mongoose');
const redisDB = require('../lib/redisManage');
let serverConfig = require('../config/serverConfig');
let ChatMemInfo = mongoose.model('ChatMemInfo');
/**
 * Chat服务
 */
class ChatMemInfoService{

    static async load(item){
        let result = await ChatMemInfo.find(item).exec();
        if(result){
            result = result[0];
        }
        return result;
    }
    static async save(item){
        let tempItem = new ChatMemInfo(item);
        await tempItem.save();
    }
    static async getCache(memID , chatID){
        let result = "{}";
        let key = this.createKey(memID,chatID);
        let item = await redisDB.get(key);
        if(item){
            result = item;
        }else {
            result = await this.load({memID:memID,chatID:chatID});
            if (result) {
                await this.setCache(key,result)
            }
        }
        return result;
    }
    static async setCache(key,item){
        await redisDB.set(key,item,serverConfig.socketCommonCacheTime)
    }
    static createKey(memID,chatID){
        return 'ChatMemInfo:' + memID + ":" + chatID;
    }

    static async searchByMemId(memId,lastTime){
        let result = [];
        if(lastTime){
            let list = await ChatMemInfo
                .find({
                    memID: memId,
                    lastReadTime: {$lt : lastTime}
                })
                .sort({lastReadTime:-1})
                .limit(serverConfig.chatPageBaseSize)
                .exec();
            if(list && list.length>0){
                let last = list[list.length-1];
                result = await ChatMemInfo
                    .find({
                        memID: memId,
                        lastReadTime: {
                            $lt : lastTime,
                            $gte : last.lastReadTime
                        }
                    })
                    .sort({lastReadTime:-1}).exec();
            }
        }else{
            let list = await ChatMemInfo.find({memID: memId})
                .sort({lastReadTime:-1})
                .limit(serverConfig.chatPageBaseSize)
                .exec();
            if(list && list.length>0){
                let last = list[list.length-1];
                result = await ChatMemInfo
                    .find({
                        memID: memId,
                        lastReadTime: {$gte : last.lastReadTime}
                    })
                    .sort({lastReadTime:-1})
                    .exec();
            }
        }
        return result;
    }
    /*
     memID:  String,
     chatID: String,
     lastReadTime: String,
     unReadSize: String,
     joinTime: String,
     status: String,
     */
    static async updateChatMemInfoReadState(memID,chatID){
        let cmi = await ChatMemInfo.updateOne({
            "memID":memID,
            "chatID":chatID,
        },{
            lastReadTime:new Date().getTime(),
            unReadSize:"0",
        });
        await redisDB.del(this.createKey(memID,chatID));
    }
    /*static async loadChatsByMemId(memId){
        let result = await Chat.find(item).exec();
        if(result){
            result = result[0];
        }
        return result;
    }*/

}
/*setTimeout(async () => {
    ChatMemInfoService.save(
        {
            memID:  '111',
            chatID: '111',
            lastReadTime: new Date().getTime(),
            unReadSize: '0',
            joinTime: new Date().getTime(),
            status: '1',
        }
    );
    ChatMemInfoService.save(
        {
            memID:  '222',
            chatID: '111',
            lastReadTime: new Date().getTime(),
            unReadSize: '0',
            joinTime: new Date().getTime(),
            status: '1',
        }
    );
});*/


module.exports = ChatMemInfoService;